package com.controller;

import com.bean.Cart;
import com.bean.CartItem;
import com.bean.Flower;
import com.service.FlowerService;
import com.service.impl.FlowerServiceImpl;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

public class CartServlet extends BaseServlet {

    private FlowerService flowerService=new FlowerServiceImpl();

    /**
     * 添加至购物车
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //System.out.println("加入购物车");
        //System.out.println("商品编号"+req.getParameter("id"));
        //获取请求参数 商品编号
        int id= WebUtils.parseInt(req.getParameter("id"),0);
        //调用flowerService.queryFlowerByFlowerid(flowerid):Flower得到鲜花产品信息
        Flower flower= (Flower) flowerService.queryFlowerByFlowerid(id);
        //将鲜花产品信息转换成为商品项
        CartItem cartItem=new CartItem(flower.getFlowerid(),flower.getFlower_name(),1,new BigDecimal(flower.getPrice()+""),new BigDecimal(flower.getPrice()+""),flower.getEnterpriseid());
        //调用cart.addItem(CartItem);添加商品项
        Cart cart= (Cart) req.getSession().getAttribute("cart");
        if(cart==null){
            cart=new Cart();
            req.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);
        //System.out.println(cart);
        //重定向商品列表页面
        //resp.sendRedirect(req.getContextPath());
        resp.sendRedirect(req.getHeader("Referer"));
    }

    /**
     * 删除商品项
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取商品
        int id= WebUtils.parseInt(req.getParameter("id"),0);
        Cart cart= (Cart) req.getSession().getAttribute("cart");
        if(cart!=null){
            cart.deleteItem(id);
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    /**
     * 清空购物车
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //
        Cart cart= (Cart) req.getSession().getAttribute("cart");
        if(cart!=null){
            cart.clear();
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    /**
     * 更改商品项数量
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id=WebUtils.parseInt(req.getParameter("id"),0);
        int count=WebUtils.parseInt(req.getParameter("count"),0);
        Cart cart= (Cart) req.getSession().getAttribute("cart");
        if(cart !=null){
            cart.updateCount(id,count);
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }
}
